
cc.Class({
    extends: cc.Component,

    properties: {
    },
    start:function(){
        this.init_laydate =false;
        this.time1 = getTime2(0);
        this.time2= getTime2(0);
        this.start_end_time();
    },
    hide:function(){
        if(!laydate_notload()&&this.init_laydate){
            if(this.time_mask.active)
                laydate.hide();
            this.time_mask.active = false;
        }
    },
    start_end_time:function() {
        this.time_mask = this.node.getChildByName('time_mask');
        this.time_mask.on('click',this.onBtnClicked.bind(this));
        this.time_mask.active = false;


        this.start_box = this.node.getChildByName('start_time').getChildByName('box').getComponent(cc.EditBox);
        this.start_box.node.on('editing-did-began',this.input_began_callback, this);

        this.end_box = this.node.getChildByName('end_time').getChildByName('box').getComponent(cc.EditBox);
        this.end_box.node.on('editing-did-began',this.input_began_callback, this);

        if(laydate_notload())return;

        var input_start = get_input_date(this.start_box.tabIndex,'start_'+this.start_box.tabIndex);
        var input_end = get_input_date(this.end_box.tabIndex,'end_'+this.end_box.tabIndex);

        laydate.reset();//重设日历控件坐标
        var self = this;
        this.start = {elem:"#start_"+this.start_box.tabIndex,format:"YYYY/MM/DD",min:laydate.now(-30*12*5),max:laydate.now(),
            istime:false,isclear: false,issure: false,istoday:false,
            choose:function(datas){
                self.end.min = datas;
                self.end.start = datas;
                self.time1 = datas;
                self.start_box.string = datas;
                input_start.style.fontSize = '25px';
                self.time_mask.active = false;
                self.select_date();
            }};
        this.end = {elem:"#end_"+this.end_box.tabIndex,format:"YYYY/MM/DD",min:laydate.now(),max:laydate.now(),
            istime:false,isclear: false,issure: false,istoday:false,
            choose:function(datas){
                self.start.max=datas;
                self.end_box.string = datas;
                self.time2 = datas;
                input_end.style.fontSize = '25px';
                self.time_mask.active = false;
                self.select_date();
            }};

        laydate(this.start);
        laydate(this.end);

        this.show_time_lable(getTime2(0),getTime2(0));

        this.init_laydate = true;
    },
    select_date:function(){
        console.log(this.time1,this.time2);
    },
    show_time_lable:function(time1,time2){
        if(this.start_box){
            this.start_box.string = time1;
            this.end_box.string = time2;
            if(laydate_notload())return;
            this.end.min = time1;
            this.end.start = time2;
        }
    },
    input_began_callback:function(event){
        this.time_mask.active = true;
    },
    onBtnClicked:function(event){
        if(!laydate_notload()&&this.init_laydate){
            if(this.time_mask.active)
                laydate.hide();
            this.time_mask.active = false;
        }
    },
});

//获取input标签 根据tabIndex值  日历控件
window.get_input_date = function(tabIndex,id_name){
    var input_list = document.getElementsByTagName("input");
    let temp = null;
    for(var i=0;i<input_list.length;i++){
        if(input_list[i].tabIndex==tabIndex){
            temp = input_list[i];
            temp.className='form-control layer-date';
            temp.id=id_name;
            temp.readonly = 'readonly';
            temp.type = 'button';
            break;
        }
    }
    return temp;
};
//判断是否已经加载第三方插件
window.laydate_notload = function(){
    if(!function(){try{return laydate,true}catch(e){}}())return true;
    return false;
};
window.getTime2 = function(offset){
    var newDate = new Date();
    newDate.setDate(newDate.getDate() + offset);
    var y = newDate.getFullYear();
    var m = newDate.getMonth() + 1;
    var d = newDate.getDate();
    return y + "/" + (m < 10 ? "0" + m : m) + "/" + (d < 10 ? "0" + d : d);
};